package com.edu.order.service.impl;

import com.api.course.CourseClient;
import com.api.sso.SSOClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.entity.HfdClassSkuInfo;
import com.edu.entity.HfdLocInfo;
import com.edu.entity.HfdOrderInfo;
import com.edu.entity.HfdUserInfo;
import com.edu.interceptor.UserInfoInterceptor;
import com.edu.order.entity.OrderPage;
import com.edu.order.mapper.HfdLocInfoMapper;
import com.edu.order.mapper.HfdOrderInfoMapper;
import com.edu.order.service.IHfdOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.utils.IdWorker;
import com.edu.vo.Result;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author jr
 * @since 2021-04-13
 */
@Service
public class HfdOrderInfoServiceImpl extends ServiceImpl<HfdOrderInfoMapper, HfdOrderInfo> implements IHfdOrderInfoService {

    @Autowired
    private IdWorker idWorker;

    @Autowired(required = false)
    private HfdOrderInfoMapper hfdOrderInfoMapper;

    @Autowired(required = false)
    private CourseClient courseClient;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Value("${topic}")
    private String TOPIC;

    @Value("${tag}")
    private String TAG;


    //订单生成
    @Override
    public Result order(OrderPage orderPage) {

        if (orderPage == null) {
            return new Result(false, 70010, "fail");
        }

        //生成订单Id
        String orderId = idWorker.nextId() + "";

        //获取用户信息
        HfdUserInfo userInfo = UserInfoInterceptor.getUserInfo();
//        HfdUserInfo userInfo =  new HfdUserInfo();
//        userInfo.setUserId(1);


        //生成订单表信息
        HfdOrderInfo orderInfo = new HfdOrderInfo();
        orderInfo.setOrderId(orderId);//订单id
        orderInfo.setUserId(userInfo.getUserId());//用户id
        orderInfo.setSkuId(orderPage.getHfdClassSkuInfo().getSkuId());//课程id
        orderInfo.setLocId(orderPage.getLocId());//地址id
        orderInfo.setTeacherId(orderPage.getHfdClassSkuInfo().getTeacherId());//教师id
        orderInfo.setPayType(orderPage.getPayType());//支付方式
        orderInfo.setStatus("0");//支付状态
        orderInfo.setOrderTime(new Date());

        //保存
        hfdOrderInfoMapper.insert(orderInfo);

        //延时消息
        rocketMQTemplate.syncSend(TOPIC + ":" + TAG,
                MessageBuilder.withPayload(orderId).build(), 2000, 4);

        return new Result(true, 70011, "success", orderId);
    }

    //超时关闭订单，修改状态
    @Override
    public Result releaseOrder(String orderId) {

        //修改订单状态
        HfdOrderInfo orderInfo = new HfdOrderInfo();
        orderInfo.setOrderId(orderId);
        orderInfo.setStatus("3");
        this.baseMapper.updateById(orderInfo);

        return new Result(true, 70021, "success");
    }


    //订单页面初始化数据
    @Override
    public Result orderPage(Integer skuId) {

        //获取课程信息(远程调用)
        HfdClassSkuInfo classSkuInfoBySkuId = courseClient.findClassSkuInfoBySkuId(skuId);

        return new Result(true, 70031, "success", classSkuInfoBySkuId);
    }

    //根据orderId查询订单
    @Override
    public HfdOrderInfo findOrderById(String orderId) {

        HfdOrderInfo orderInfo = hfdOrderInfoMapper.selectById(orderId);

        return orderInfo;
    }

    //购买成功后添加
    @Override
    public Result addUserVideo(HfdOrderInfo hfdOrderInfo) {
        int add = hfdOrderInfoMapper.addUserVideo(hfdOrderInfo);
        if (add > 0) {
            return new Result(true, 70031, "success");
        } else {
            return new Result(false, 70031, "fail");
        }
    }


}
